// pages/search/search.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchShow: false,
    keyword: '',
    searchBookList: [],
    hotSearch: [],
    scHistory: [],
    notData: "",
    deleteShow: false
  },

  // 获取搜索关键字
  getSearchValue(e) {
    if (!e.detail.value) {
      this.setData({
        searchShow: true
      })
      return
    }
    this.setData({
      keyword: (e.detail.value).trim()
    })

  },

  getKw(e) {
    this.setData({
      keyword: e.target.dataset.kw
    })
    this.onSearch()
    console.log(this.data.keyword, 'aaa');

  },

  // 搜索
  onSearch() {
    if (!this.data.keyword) {
      return
    }
    let _this = this
    this.setData({
      searchShow: false
    })
    wx.showLoading({
      title: '搜索中',
    })

    db.collection("searchHistory").where({
      scKeyword: _this.data.keyword
    }).get().then((res) => {
      console.log(res, 'res3');
      if (!res.data.length) {
        wx.request({
          url: `https://apis.netstart.cn/yunyuedu/search/book.json?keyword=${_this.data.keyword}`,
          method: 'GET',
          success: (res) => {
            console.log(res.data.data.books, 'book')
            console.log(res, 'res3333')
            let list = res.data.data.books
            if (!list) {
              wx.hideLoading()
              _this.setData({
                searchBookList: [],
                notData: '查询无果'
              })
              return
            }
            wx.cloud.database().collection('searchHistory').add({
              data: {
                scKeyword: _this.data.keyword,
                searchResultBook: list
              },
              success(res) {
                console.log('添加成功', res)
              },
              fail(res) {
                console.log('添加失败', res)
              }
            })
            _this.setData({
              searchBookList: list
            })
            wx.hideLoading()
          },
          fail: (res) => {
            console.log(res, '查询有误');
            wx.hideLoading()
          }
        })
      } else {
        let list = res.data[0]
        _this.setData({
          searchBookList: list.searchResultBook
        })
        if (!_this.data.searchBookList.length) {
          this.setData({
            notData: '查询无果'
          })
        }
        wx.hideLoading()
      }

    })

  },

  // 去详情页
  toDetail(e) {
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url: `/pages/detail/detail?id=${e.currentTarget.dataset.id}&title=${e.currentTarget.dataset.title}`,
    })
  },

  
  deleteSc() {
    console.log(this.data.deleteShow);
    this.setData({
      deleteShow: !this.data.deleteShow
    })
  },

  // 删除某一个
  removeSc(evt) {
    let kw = evt.currentTarget.dataset.kw
    db.collection('searchHistory').where({
      scKeyword: kw
    }).remove()

    this.onShow()
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    db.collection('hotSearch').where({}).get().then((res) => {
      if (!res.data.length) {
        wx.request({
          url: 'https://apis.netstart.cn/yunyuedu/search/getHotWords',
          success: (res) => {
            let list = res.data.list
            db.collection('hotSearch').add({
              data: {
                list
              }
            })
            this.setData({
              hotSearch: list
            })
          }
        })
      } else {
        let list = res.data[0].list
        this.setData({
          hotSearch: list
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (!this.data.keyword) {
      this.setData({
        searchShow: true
      })
    }

    db.collection('searchHistory').where({}).get().then((res) => {
      console.log(res.data);
      this.setData({
        scHistory: res.data
      })
      console.log(this.data.scHistory, 'sc');
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})